//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.未分类;

import java.util.HashMap;
import java.util.Map;

public class demo_781 {
    public demo_781() {
    }

    public static void main(String[] args) {
        int[] anwers = new int[]{0, 3, 2, 0, 3, 3, 4, 2, 4, 3, 2, 4, 4, 3, 0, 1, 3, 4, 4, 3};
        System.out.println(numRabbits(anwers));
    }

    public static int numRabbits(int[] answers) {
        HashMap<Integer, Integer> map = new HashMap();
        int count = 0;

        for(int a : answers) {
            map.put(a, (Integer)map.getOrDefault(a, 0) + 1);
        }

        for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
            int key = (Integer)entry.getKey();
            int value = (Integer)entry.getValue();
            count += (value + key) / (key + 1) * (key + 1);
        }

        return count;
    }

    public int numRabbits2(int[] answers) {
        int ans = 0;
        Map<Integer, Integer> left = new HashMap();

        for(int x : answers) {
            int c = (Integer)left.getOrDefault(x, 0);
            if (c == 0) {
                ans += x + 1;
                left.put(x, x);
            } else {
                left.put(x, c - 1);
            }
        }

        return ans;
    }
}
